﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GTAuto.DAL.Entities;
using GTAuto.DAL.Enums;
using GTAuto.DAL.Extensions;

namespace GTAuto.DAL.DAL
{
    public class DALFirmCategory : DALBase
    {
        public static DALFirmCategory Singleton = new DALFirmCategory();

        public GTFirmCategory Add(GTFirmCategory firmCategory)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var newCategory = new FirmCategory();

                newCategory.Name = firmCategory.Name;
                newCategory.Logo = firmCategory.Logo;
                newCategory.Active = firmCategory.Active;
                db.FirmCategories.InsertOnSubmit(newCategory);
                db.SubmitChanges();
                firmCategory.Id = newCategory.Id;
                return firmCategory;
            }
        }

        public GTFirmCategory Update(GTFirmCategory adCategory)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var category = db.FirmCategories.SingleOrDefault(x => x.Id == adCategory.Id);
                category.Name = adCategory.Name;
                category.Logo = adCategory.Logo;
                category.Active = adCategory.Active;


                db.SubmitChanges();

                return adCategory;
            }
        }


        public GTFirmCategory Get(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var brand = db.FirmCategories.SingleOrDefault(x => x.Id == id);
                GTFirmCategory gtBrand = new GTFirmCategory();
                gtBrand.UpdateTo(brand);
                return gtBrand;
            }
        }

        public List<GTFirmCategory> GetPaged(int page, int count, out int itemsCount)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.FirmCategories
                 orderby p.Name
                 select p;


                var brands = brds.ToList();

                itemsCount = brands.Count;

                var ret = new List<GTFirmCategory>();
                foreach (var brand in brands.Page(page, count))
                {
                    GTFirmCategory gtBrand = new GTFirmCategory();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }

     
        public void Delete(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var singleOrDefault = db.FirmCategories.SingleOrDefault(x => x.Id == id);

                if (singleOrDefault != null)
                {
                    db.FirmCategories.DeleteOnSubmit(singleOrDefault);
                    db.SubmitChanges();
                }
            }
        }

    }
}
